package com.wutoon.etl.service.impl;

import com.alibaba.fastjson.JSON;
import com.wutoon.etl.aop.PowerJobLogReport;
import com.wutoon.etl.constant.CommonConstant;
import com.wutoon.etl.handler.HttpPushHandler;
import com.wutoon.etl.mapper.BizOperateRecordManage;
import com.wutoon.etl.pojo.DataSourceDO;
import com.wutoon.etl.pojo.PowerJobResult;
import com.wutoon.etl.pojo.MapReduceShard;
import com.wutoon.etl.pojo.Db2HttpTask;
import com.wutoon.etl.mapper.DataSourceManage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.sql.*;

/**
 * @author 武兴云/72176468
 * @version 1.0
 * @date 2023/11/13 4:23 PM
 * @description 运维服务
 */
@Service
@Slf4j
public class DataPushService {

    @Resource
    private DataSourceManage dataSourceManage;

    @Resource(name = "outRestTemplate")
    private RestTemplate outRestTemplate;

    @Resource
    private BizOperateRecordManage bizOperateRecordManage;

    public PowerJobResult<?> db2http(MapReduceShard shard) {
        log.info("DataPushService pushData start, param:{}", JSON.toJSONString(shard));
        PowerJobLogReport.report("DataPushService start");
        try {
            Db2HttpTask task = JSON.parseObject(shard.getCustomParam().toJSONString(), Db2HttpTask.class);
            // 校验参数
            if (task.getTargetSourceConfig() == null || StringUtils.isEmpty(task.getSourceCode()) ||
                    StringUtils.isEmpty(task.getQuerySql()) || task.getBatchInsertSize() == null
                    || task.getFetchSize() == null || task.getConcurrencySize() == null) {
                return new PowerJobResult<>(false, "非法参数, 必填参数为空");
            }
            DataSourceDO dataSource = dataSourceManage.queryByCode(task.getSourceCode());
            task.setDataSource(dataSource);

            HttpPushHandler handler = new HttpPushHandler(task, shard, outRestTemplate, bizOperateRecordManage);
            handler.start();
            log.info("DataPushService pushData end");
            PowerJobLogReport.report("DataPushService end");
            return new PowerJobResult<>(true, shard);
        } catch (Exception e) {
            log.error("DataPushService pushData error:", e);
            return new PowerJobResult<>(false, e.getMessage());
        }
    }


    /**
     * 获取数据源
     *
     * @param dataSourceDO DataSourceDO
     * @return Connection
     * @author 武兴云/72176468
     * @date 2024/3/11 17:05
     * @update_by 武兴云/72176468
     * @update_at 2024/3/11 17:05
     * @creed if you have doubt , please contact me !!!
     */
    public Connection connect(DataSourceDO dataSourceDO) {
        try {
            Class.forName(dataSourceDO.getDriver());
            DriverManager.setLoginTimeout(CommonConstant.TIMEOUT_SECONDS);
            return DriverManager.getConnection(dataSourceDO.getJdbcUrl(), dataSourceDO.getUsername(), dataSourceDO.getPassword());
        } catch (Exception e) {
            throw new RuntimeException("initialize datasource connection error");
        }
    }
}
